module Utils

using Images


export pixelValue
export mapToImage
export save_img

"""
#Arguments

- `size :: Int64`: size of the image in pixel
- `xmin :: Float64`: coordinate of left edge
- `xmax`
- `ymin :: Float64`: coordinate of bottom
- `ymax :: Float64`: coordinate of top
- `x, y :: Int64`: index of the target pixel 
"""
function pixelValue(size, xmin, xmax, ymin, ymax, x, y) :: Complex{Float64}
     x_magnitude = xmin + ((xmax - xmin)/size) * x
     y_magnitude = ymin + ((ymax - ymin)/size) * y
     return complex(x_magnitude, y_magnitude)
end


function mapToImage(renderer, cmap::Array{Complex{Float64}, 2}) :: Array{RGB{N0f8}, 2}
    pixels = size(cmap)[1]

    image = Array{RGB{Float32}, 2}(undef, pixels, pixels)
    
    for i in 1:pixels
        for j in 1:pixels
            r, g, b = renderer(cmap[i, j])
            image[i, j] = RGB{N0f8}(r, g, b)
            
        end
    end

    return image
end

function save_img( path::String, img::Array{RGB{N0f8}, 2}, verbose::Bool)
    if verbose
        println("Generating: $path")
    end
    save(path, img)

end



end
